package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TudaOrder;
import com.ruoyi.system.domain.TudaOrderTransfer;
import com.ruoyi.system.model.result.OrderRecordResult;
import com.ruoyi.system.model.result.OrderTotalAmountResult;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITuDaFinanceService;
import com.ruoyi.system.service.ITudaOrderRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TuDaFinanceController
 * @Description TODO
 * @Author tuda
 * @Date 2023/7/31 10:06
 * @Version 1.0
 **/
@Controller
@RequestMapping("/system/finance")
public class TuDaFinanceController extends BaseController {

    private String prefix = "system/finance";

    @Resource
    private ITuDaFinanceService financeService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ITudaOrderRecordService orderRecordService;

    @RequiresPermissions("system:finance:transport")
    @GetMapping("/transport")
    public String transport(ModelMap mmap)
    {
        return prefix + "/transport";
    }

    @RequiresPermissions("system:finance:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/finance";
    }

    /**
     * 查询财务运单列表
     */
    @RequiresPermissions("system:finance:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderRecordResult recordResult)
    {
        startPage();
        List<OrderRecordResult> list = orderRecordService.selectOrderRecordList(recordResult);
        return getDataTable(list);
    }

    @PostMapping("/total")
    @ResponseBody
    public OrderTotalAmountResult selectTotalAmount(TudaOrder tudaOrder){
        OrderTotalAmountResult totalAmountResult = financeService.selectTotalAmount(tudaOrder);
        return totalAmountResult;
    }

    @RequiresPermissions("system:finance:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        TudaOrder tudaOrder = financeService.selectTudaOrderById(id);
        String[] str = tudaOrder.getCarImgs().split(",");
        List<String> carList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            carList.add(str[i]);
        }
        tudaOrder.setCarImgList(carList);
        String[] str2 = tudaOrder.getContractImgs().split(",");
        List<String> contractImgsList = new ArrayList<>();
        for (int i = 0; i < str2.length; i++) {
            contractImgsList.add(str2[i]);
        }
        tudaOrder.setContractImgList(contractImgsList);
        if(tudaOrder.getPayType() == 1){
            tudaOrder.setPayTypeValue("到付");
        }else if(tudaOrder.getPayType() == 2){
            tudaOrder.setPayTypeValue("现付");
        }else {
            tudaOrder.setPayTypeValue("欠付");
        }
        mmap.put("tudaOrder", tudaOrder);
        return prefix + "/detail";
    }

    /**
     * 导出货运订单列表
     */
    @RequiresPermissions("system:finance:export")
    @Log(title = "财务运单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TudaOrder tudaOrder)
    {
        List<TudaOrder> list = financeService.selectTudaOrderList(tudaOrder);
        ExcelUtil<TudaOrder> util = new ExcelUtil<TudaOrder>(TudaOrder.class);
        return util.exportExcel(list, "财务运单数据");
    }

    /**
     * 修改货运订单
     */
    @RequiresPermissions("system:finance:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TudaOrder tudaOrder = financeService.selectTudaOrderById(id);
        String[] str = tudaOrder.getCarImgs().split(",");
        List<String> carList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            carList.add(str[i]);
        }
        tudaOrder.setCarImgList(carList);
        String[] str2 = tudaOrder.getContractImgs().split(",");
        List<String> contractImgsList = new ArrayList<>();
        for (int i = 0; i < str2.length; i++) {
            contractImgsList.add(str2[i]);
        }
        tudaOrder.setContractImgList(contractImgsList);
        mmap.put("userList", sysUserService.queryUserByRole());
        mmap.put("tudaOrder", tudaOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存货运订单
     */
    @RequiresPermissions("system:finance:edit")
    @Log(title = "货运订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TudaOrder tudaOrder)
    {
        return toAjax(financeService.updateTudaOrder(tudaOrder));
    }

    /**
     * 确认返款
     */
    @RequiresPermissions("system:finance:refund")
    @Log(title = "确认返款/收款", businessType = BusinessType.UPDATE)
    @PostMapping( "/refund")
    @ResponseBody
    public AjaxResult refund(String id)
    {
        return toAjax(financeService.refund(id));
    }

    @RequiresPermissions("system:finance:completion")
    @Log(title = "订单已完结", businessType = BusinessType.UPDATE)
    @PostMapping( "/completion")
    @ResponseBody
    public AjaxResult completion(String id)
    {
        return toAjax(financeService.orderCompletion(id));
    }


    /**
     * 运单清单列表
     */
    @RequiresPermissions("system:finance:detailed")
    @GetMapping("/detailed/{id}")
    public String detailed(@PathVariable("id") String id, ModelMap mmap)
    {
        List<TudaOrderTransfer> list = financeService.detailed(id);
        BigDecimal totalAmount = new BigDecimal(0.00);
        for (TudaOrderTransfer orderTransfer : list){
            totalAmount = totalAmount.add(orderTransfer.getOwes());
        }
        mmap.put("ids", id);
        mmap.put("totalAmount", totalAmount);
        mmap.put("orderTransferList", list);
        return prefix + "/detailed";
    }

    /**
     * 批量结算司机运单
     */
    @RequiresPermissions("system:finance:settlement")
    @Log(title = "批量结算运单", businessType = BusinessType.UPDATE)
    @PostMapping( "/settlement")
    @ResponseBody
    public AjaxResult settlement(String ids)
    {
        return toAjax(financeService.settlement(ids));
    }

    /**
     * 批量汇总业务员提成+明细
     */
//    @RequiresPermissions("system:finance:salesman")
//    @PostMapping( "/salesman")
//    @ResponseBody
//    public AjaxResult salesman(String ids)
//    {
//        return toAjax(financeService.settlement(ids));
//    }

    /**
     * 运单清单列表
     */
    @RequiresPermissions("system:finance:salesman")
    @GetMapping("/salesman/{id}")
    public String salesman(@PathVariable("id") String id, ModelMap mmap)
    {
        List<TudaOrder> list = financeService.salesmanList(id);
        List<TudaOrderTransfer> orderTransferList = financeService.salesmanTransferList(id);
        BigDecimal totalProfit = new BigDecimal(0.00);
        BigDecimal totalCommission = new BigDecimal(0.00);
        for (TudaOrder order : list){
            totalProfit = totalProfit.add(order.getProfit());
            totalCommission = totalCommission.add(order.getCommission());
        }
        mmap.put("orderTransferList", orderTransferList);
        mmap.put("ids", id);
        mmap.put("totalProfit", totalProfit);
        mmap.put("totalCommission", totalCommission);
        mmap.put("orderList", list);
        return prefix + "/salesman";
    }

    /**
     * 运单杂费结算
     */
    @Log(title = "运单杂费结算", businessType = BusinessType.UPDATE)
    @PostMapping( "/updateOrderFee")
    @ResponseBody
    public AjaxResult updateOrderFee(Long orderId, String feeState, String feeName, String notes, BigDecimal amount, String feeAmount)
    {
        return toAjax(financeService.updateOrderFee(orderId, feeState, feeName, notes, amount, feeAmount));
    }


}
